<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>授权成功 - 即将跳转</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
            color: #333;
            text-align: center;
        }
        .container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            padding: 40px;
            max-width: 600px;
            width: 100%;
        }
        h1 {
            color: #333;
            margin-bottom: 30px;
        }
        .spinner {
            width: 50px;
            height: 50px;
            border: 5px solid #f3f3f3;
            border-radius: 50%;
            border-top: 5px solid #667eea;
            animation: spin 1s linear infinite;
            margin: 30px auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .success-message {
            background: #d4edda;
            border-left: 4px solid #28a745;
            padding: 15px;
            margin: 20px 0;
            border-radius: 5px;
            color: #155724;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔐 授权成功</h1>
        
        <div class="success-message">
            <strong>✅ 已成功获取访问令牌！</strong>
            <p>正在保存令牌并跳转到消息管理系统，请稍候...</p>
        </div>
        
        <div class="spinner"></div>
    </div>

    <!-- 接收后端传来的令牌数据 -->
    <script th:inline="javascript">
        // 从后端获取令牌数据
        const tokenResponse = /*[[${tokenResponse}]]*/ {};
        
        // 保存令牌数据到本地存储
        if (tokenResponse && tokenResponse.access_token) {
            // 确保清除之前可能存在的token数据
            localStorage.removeItem('oauth2_access_token');
            localStorage.removeItem('oauth2_refresh_token');
            localStorage.removeItem('oauth2_token_type');
            localStorage.removeItem('oauth2_scope');
            localStorage.removeItem('oauth2_expires_in');
            
            // 保存新的token数据
            localStorage.setItem('oauth2_access_token', tokenResponse.access_token);
            localStorage.setItem('oauth2_refresh_token', tokenResponse.refresh_token);
            localStorage.setItem('oauth2_token_type', tokenResponse.token_type);
            localStorage.setItem('oauth2_scope', tokenResponse.scope);
            localStorage.setItem('oauth2_expires_in', tokenResponse.expires_in);
            
            console.log('令牌已成功保存到本地存储');
            
            // 验证token已成功保存
            const savedToken = localStorage.getItem('oauth2_access_token');
            console.log('验证localStorage结果:', savedToken ? '成功' : '失败');
            
            // 不管localStorage是否成功，都使用URL参数跳转，确保token传递
            // 延迟一小段时间后跳转，让用户能看到成功信息
            setTimeout(function() {
                // 同时使用URL参数传递token，确保后端和前端都能拿到
                window.location.href = '/messages?token=' + encodeURIComponent(tokenResponse.access_token) + 
                                      '&refresh_token=' + encodeURIComponent(tokenResponse.refresh_token || '') + 
                                      '&token_type=' + encodeURIComponent(tokenResponse.token_type || 'Bearer') +
                                      '&expires_in=' + encodeURIComponent(tokenResponse.expires_in || '3600');
            }, 1500);
        } else {
            console.error('未收到有效的令牌数据');
            // 如果没有收到令牌数据，跳转回首页
            setTimeout(function() {
                window.location.href = '/?error=no_token_data';
            }, 2000);
        }
    </script>
</body>
</html>
